package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.FileShare;
import com.kaishengit.entity.Files;
import com.kaishengit.util.DBhelp;
import com.kaishengit.util.Pager;
import com.kaishengit.util.RowMapper;

public class FileDao {
	DBhelp<Files> help = new DBhelp<Files>();
	public void insert(){
		
		
	}
	public Pager findAll(String fsid, int presentNum) {
		String sql = "select f_id,f_name,f_size,f_createTime,fsid,mid,f_pathName from file where fsid = ? limit ?,?";
		Pager pager = new Pager(getTotalCounts(fsid),5,presentNum);
		List<Files> list = help.executeForList(sql, new FileRowMapper(), fsid,pager.getStartIndex(),pager.getPerSize());
		FileShareDao fsdao = new FileShareDao();
		FileShare fs = fsdao.findById(fsid);
		pager.setResult(list);
		return pager;
	}
	private int getTotalCounts(String fsid) {
		String sql = "select count(*) from file where fsid = ?";
		return help.executeQueryForCount(sql, fsid);
	}
	private class FileRowMapper implements RowMapper<Files>{

		public Files mapper(ResultSet rs) throws SQLException {
			Files file = new Files();
			file.setF_id(rs.getString("f_id"));
			file.setF_name(rs.getString("f_name"));
			file.setF_size(rs.getString("f_size"));
			file.setF_createTime(rs.getString("f_createTime"));
			file.setF_pathName(rs.getString("f_pathName"));
			file.setFsid(rs.getString("fsid"));
			file.setMid(rs.getString("mid"));
			return file;
		}
		
	}

}
